<?php
/**
 * @filename boot.php
 * @author anzai <236049524@qq.com>
 * @version 1.0
 * @copyright http://www.judianyun.com 2013
 *
 * Modified at : 2013-2-6 07:57:59
 *
 * EasyPHP 框架入口文件
 *
 */

//记录系统运行开始时间
$GLOBALS['__StartTime__'] = microtime(TRUE);

if(version_compare(PHP_VERSION,'5.2.0','<'))  die('PHP Version must be > 5.2.0 !');

@ header('Expires: Thu, 01 Jan 1970 00:00:01 GMT');
@ header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
@ header('Cache-Control: no-cache, must-revalidate, max-age=0');
@ header('Pragma: no-cache');
if(function_exists('date_default_timezone_set')){
	date_default_timezone_set('PRC');
}

//开启DEBUG模式标识，true开启
if (!defined('APP_DEBUG')) define('APP_DEBUG', false);

//系统运行模式，1开发者模式，0正式上线模式
if (!defined('RUNNING_MODE')) define('RUNNING_MODE', 1);

//设置错误处理机制
error_reporting(E_ERROR | E_WARNING | E_PARSE);

//开启或关闭报错功能
ini_set('display_errors',       RUNNING_MODE ? 1 : 0);

//页面输出编码
if (!defined('OUT_CHARSET')) define('OUT_CHARSET', 'UTF-8');

//项目名称
if(!defined('APP_NAME')) define('APP_NAME', 'newApp');

//目录分割符
if(!defined('DS')) define('DS', DIRECTORY_SEPARATOR);

//项目根目录
if(!defined('ROOT_PATH')) define('ROOT_PATH', dirname($_SERVER['SCRIPT_FILENAME']).DS);

//框架存放目录
if(!defined('EASYPHP_PATH')) define('EASYPHP_PATH',dirname(__FILE__).DS);

//项目存放目录
if(!defined('APP_PATH')) define('APP_PATH',dirname($_SERVER['SCRIPT_FILENAME']).DS);

//总临时文件存放目录
if(!defined('TEMP_ROOT_PATH')) define('TEMP_ROOT_PATH', ROOT_PATH . 'temp' . DS);

if (is_dir(TEMP_ROOT_PATH)){
    if (!is_writable(TEMP_ROOT_PATH)){
        die('No write permission: ' . TEMP_ROOT_PATH);
    }elseif (!is_readable(TEMP_ROOT_PATH)){
        die('No read permission: ' . TEMP_ROOT_PATH);
    }
}else{
    die('Directory does not exist: ' . TEMP_ROOT_PATH);
}

//项目临时文件存放目录
if(!defined('TEMP_PATH')) define('TEMP_PATH', TEMP_ROOT_PATH . '~' . md5(APP_PATH) . DS);

//项目控制器文件存放目录
if(!defined('LIB_PATH')) define('LIB_PATH', APP_PATH . 'lib' . DS);

//项目配置文件、函数库、类库等存放目录
if(!defined('INCLUDES_PATH')) define('INCLUDES_PATH', APP_PATH . 'includes' . DS);

//端口
define('HTTP_PORT', $_SERVER['SERVER_PORT']);

//主机协议
define('HTTP_PROTOCOL', HTTP_PORT == '443' ? 'https://' : 'http://');

$domain = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'].(HTTP_PORT==80 || HTTP_PORT==443 ? '' : HTTP_PORT));

define('DOMAIN_NAME', $domain);

if (!defined('HTTP_HOST')){
	$suffix = str_replace('\\', '/', substr(ROOT_PATH, strlen(dirname($_SERVER['SCRIPT_FILENAME']))));
	define('HTTP_HOST', HTTP_PROTOCOL.$domain.str_replace('//', '/', '/'.($suffix!='/'?$suffix:'')));
	
	//当前访问的域名
	define('HTTP_DOMAIN', HTTP_PROTOCOL . $domain);
}else{
	define('HTTP_DOMAIN', substr(HTTP_HOST, 0 ,-1));
}

define('HTTP_URL', HTTP_PROTOCOL . $domain . $_SERVER['REQUEST_URI']); //当前完整URL地址

//访问来源
define('HTTP_REFERER', isset($_SERVER['HTTP_REFERER']) && HTTP_URL != $_SERVER['HTTP_REFERER']  ? $_SERVER['HTTP_REFERER'] : '');

//定义框架内置文件存放目录全局常量
define('__INCLUDES_PATH__', EASYPHP_PATH . 'Includes' . DS);		//项目语言包、函数库、配置文件存放目录
define('__LANG_PATH__', __INCLUDES_PATH__ . 'Lang' . DS);			//框架内置语言包存放目录
define('__FUNCTIONS_PATH__', __INCLUDES_PATH__ . 'Functions' . DS);	//框架内置函数库存放目录
define('__EXCEPTION_PATH__', __INCLUDES_PATH__ . 'Exception' . DS);
define('__CORE_PATH__', EASYPHP_PATH . 'Core' . DS);				//核心文件存放目录
define('__DB_PATH__', EASYPHP_PATH . 'Db' . DS);					//数据库引擎存放目录
define('__VIEW_PATH__', EASYPHP_PATH . 'View' . DS);				//模板引擎存放目录
define('__CACHE_PATH__', EASYPHP_PATH . 'Cache' . DS);				//数据缓存管理类库存放目录
define('__EXT_PATH__', EASYPHP_PATH . 'Ext' . DS);					//扩展类存放目录

set_include_path(get_include_path() . PATH_SEPARATOR . __EXT_PATH__);

if (!defined('VAR_MODULE'))		define('VAR_MODULE', 'm');			//控制器变量名
if (!defined('VAR_ACTION'))		define('VAR_ACTION', 'a');			//执行动作变量名
if (!defined('DF_MODULE'))		define('DF_MODULE', 'index');		//默认调用控制器
if (!defined('DF_ACTION')) 		define('DF_ACTION', 'index');		//默认执行动作
if (!defined('ACTION_SUFFIX')) 	define('ACTION_SUFFIX', 'Action');	//控制器变量名
if (!defined('MODEL_SUFFIX')) 	define('MODEL_SUFFIX', 'Model');	//执行动作变量名

//设置输出页面字符集
header('Content-type: text/html; charset='.OUT_CHARSET);

defined('MAGIC_QUOTES_GPC') or define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());

//自动载入项目公共函数库
$funcFile = INCLUDES_PATH . 'function.php';
if (is_file($funcFile)){
    require_once $funcFile;
}

//加载核心文件
_load_core_file();

function _load_core_file(){
	is_dir(TEMP_PATH) or mkdir(TEMP_PATH, 0755, true);
	$corefiles = array(
		__CORE_PATH__ . 'Base.class.php',
		__CORE_PATH__ . 'Route.class.php',
		__CORE_PATH__ . 'Registry.class.php',
		__CORE_PATH__ . 'App.class.php',
		__CORE_PATH__ . 'Action.class.php',
		__CORE_PATH__ . 'Model.class.php',
		__CORE_PATH__ . 'Profile.class.php',
		__CORE_PATH__ . 'Form.class.php',
		__CORE_PATH__ . 'E.class.php',
		__CORE_PATH__ . 'Helper.class.php',
		__FUNCTIONS_PATH__ . 'Common.php',
		__FUNCTIONS_PATH__ . 'Compat.php'
	);
	
	//正式上线对核心文件进行压缩处理
	if (!RUNNING_MODE){
		$runfile = TEMP_ROOT_PATH . '~running.php';
		if (!file_exists($runfile)){
			$php = '';
			foreach ($corefiles as $file){
				if (file_exists($file)){
					//返回已删除 PHP 注释以及空白字符的源代码文件
					$php .= php_strip_whitespace($file);
				}
			}
			file_put_contents($runfile, $php);
		}
		require_once $runfile;
	}else{
		foreach ($corefiles as $file){
			include_once $file;
		}
	}
	
	//进行路由解析
	$params = Route::dispath();
	
	if(!defined('ACTION_NAME')) define('ACTION_NAME', $params['act']);
	if(!defined('MODULE_NAME')) define('MODULE_NAME', $params['mod']);
	
	$GLOBALS['MODULE_NAME'] = MODULE_NAME;
	$GLOBALS['ACTION_NAME'] = ACTION_NAME;
	
	unset($corefiles, $params);
}